package com.nannybear.Controller;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nannybear.R;
import com.nannybear.View.RangeSeekBar;

/**
 * \brief Controller of the settings Screen
 * \author Audrey LOISEAU
 * \date 22-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class ParametersController {
    /********** DECLARATIONS **********/
    // Variables
    private int soundValue;
    private int lightValue_dark_dim = 0;
    private int lightValue_dim_bright = 0;
    static private int SOUND_DEFAULT = 148;
    static private int LIGHT_DEFAULT_DARK_DIM = 55;
    static private int LIGHT_DEFAULT_DIM_BRIGHT = 80;
    static private int GRAPHIC_TIME_DEFAULT = 1;    //1min
    private String layout_open = null;

    // Classes
	private final ViewPuppeteer viewPuppeteer;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param viewPuppeteer The ViewPuppeteer class
     */
	public ParametersController (ViewPuppeteer viewPuppeteer) {
		this.viewPuppeteer = viewPuppeteer;
	}

    /**
     * \brief Initialization of the baby information interface (creation widget and listener)
     */
    private void initBabyInfo(){
        final EditText baby_name = (EditText) this.viewPuppeteer.getmActivity().findViewById(R.id.edit_name);
        RadioButton girl_btn = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.girl_button);
        RadioButton boy_btn = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.boy_button);
        RadioButton neutral_btn = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.neutral_button);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBb_gender((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.bb_gender));

        // Listener
        baby_name.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    if (baby_name.getText().toString().trim().length() > 0) {
                        viewPuppeteer.getmActivity().getYoda().savedPreferences("name", baby_name.getText().toString());
                    }
                    // close the keyboard
                    InputMethodManager imm = (InputMethodManager) getViewPuppeteer().getmActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(baby_name.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
        neutral_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getSettingsScreen().changeGenderBaby(0);
                viewPuppeteer.getmActivity().getYoda().savedPreferences("gender","neutral");
            }
        });
        girl_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getSettingsScreen().changeGenderBaby(1);
                viewPuppeteer.getmActivity().getYoda().savedPreferences("gender", "girl");
            }
        });
        boy_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getSettingsScreen().changeGenderBaby(2);
                viewPuppeteer.getmActivity().getYoda().savedPreferences("gender", "boy");
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBaby_name(baby_name);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBtn_girl(girl_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBtn_boy(boy_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBtn_neutral(neutral_btn);
    }

    /**
     * \brief Initialization of the sound interface (creation widget and listener)
     */
    private void initSound(){
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_quiet_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_quiet_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_noise_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_noise_start));
        Button default_sound = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.default_sound_btn);

        // Listener
        final SeekBar sound_seekbar = (SeekBar) this.viewPuppeteer.getmActivity().findViewById(R.id.sound_seekBar);

        sound_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            boolean cursor_pressed = false;
            int soundThreshold = 0;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
                this.soundThreshold = (progressValue+120);
                if(cursor_pressed && fromUser){
                    soundValue = soundThreshold;
                    viewPuppeteer.getmActivity().getSettingsScreen().displayValueSound(soundThreshold);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                cursor_pressed=true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                cursor_pressed=false;
                sendNewSoundThreshold();
            }
        });
        default_sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getSettingsScreen().displayValueSound(SOUND_DEFAULT);
                sound_seekbar.setProgress(SOUND_DEFAULT - 120);
                soundValue = SOUND_DEFAULT;
                sendNewSoundThreshold();
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getSettingsScreen().setDefault_sound(default_sound);

        // Set the button to initialized place for the sound seekBar
        sound_seekbar.setProgress((int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("soundValue", SOUND_DEFAULT) - 120);
    }

    /**
     * \brief Initialization of the brightness interface (creation widget and listener)
     */
    private void initBrightness(){
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dark_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dark_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dim_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dim_start));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dim_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dim_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_bright_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_light_start));
        LinearLayout lightSeekBar_layout = (LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.light_seekbar);
        Button default_light = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.default_light_btn);

        // create RangeSeekBar as Integer range between 0 and 100
        final RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(0, 100, this.viewPuppeteer.getmActivity().getBaseContext());
        // Set the button to initialized place for the light seekBar
        seekBar.setNormalizedMinValue(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("lightValue_dark_dim", LIGHT_DEFAULT_DARK_DIM) / 100d);
        seekBar.setNormalizedMaxValue(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("lightValue_dim_bright", LIGHT_DEFAULT_DIM_BRIGHT) / 100);

        // Listener
        default_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getSettingsScreen().displayValueLight(LIGHT_DEFAULT_DARK_DIM, LIGHT_DEFAULT_DIM_BRIGHT);
                seekBar.setNormalizedMinValue(LIGHT_DEFAULT_DARK_DIM/100d);
                seekBar.setNormalizedMaxValue(LIGHT_DEFAULT_DIM_BRIGHT/100d);
                lightValue_dark_dim = LIGHT_DEFAULT_DARK_DIM;
                lightValue_dim_bright = LIGHT_DEFAULT_DIM_BRIGHT;
                sendNewLightThreshold();
            }
        });
        seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer valueDarkDim, Integer valueDimBright, boolean sendData) {
                int lightThreshold_dark_dim = valueDarkDim;
                int lightThreshold_dim_bright = valueDimBright;
                lightValue_dark_dim = lightThreshold_dark_dim;
                lightValue_dim_bright = lightThreshold_dim_bright;
                getViewPuppeteer().getmActivity().getSettingsScreen().displayValueLight(lightThreshold_dark_dim, lightThreshold_dim_bright);
                if(sendData){
                    sendNewLightThreshold();
                }
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getSettingsScreen().setLight_seekbar(lightSeekBar_layout);
        this.viewPuppeteer.getmActivity().getSettingsScreen().getLight_seekbar().removeAllViews();
        this.viewPuppeteer.getmActivity().getSettingsScreen().getLight_seekbar().addView(seekBar);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setDefault_light(default_light);
    }

    /**
     * \brief Initialization of the personalization interface (creation widget and listener)
     */
    private void initPerso(){
        final CheckBox check_popUp = (CheckBox) this.getViewPuppeteer().getmActivity().findViewById(R.id.check_popUp);
        final CheckBox check_sound = (CheckBox) this.getViewPuppeteer().getmActivity().findViewById(R.id.check_sound);
        final CheckBox check_vibrator = (CheckBox) this.getViewPuppeteer().getmActivity().findViewById(R.id.check_vibrator);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setCheckBox_sound_txt((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.choice_sound));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setCheckBox_vibrator_txt((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.choice_vibrator));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTime_hour_graph((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_hour_graph));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTime_min_graph((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_min_graph));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_1_timeline((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_1_hour));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_1_timeline((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_1_min));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_2_timeline((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_2_hour));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_2_timeline((TextView) this.getViewPuppeteer().getmActivity().findViewById(R.id.time_2_min));
        final ImageButton edit_hour_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_up);
        final ImageButton edit_hour_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_down);
        final ImageButton edit_min_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_up);
        final ImageButton edit_min_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_down);
        RadioButton color_fuchsia = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.radio_fuchsia);
        RadioButton color_red = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.radio_red);
        RadioButton color_green = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.radio_green);
        RadioButton color_blue = (RadioButton) this.viewPuppeteer.getmActivity().findViewById(R.id.radio_blue);
        final ImageButton hour_timeline1_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_timeline_1_up);
        final ImageButton min_timeline1_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_timeline_1_up);
        final ImageButton hour_timeline2_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_timeline_2_up);
        final ImageButton min_timeline2_up = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_timeline_2_up);
        final ImageButton hour_timeline1_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_timeline_1_down);
        final ImageButton min_timeline1_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_timeline_1_down);
        final ImageButton hour_timeline2_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.hour_timeline_2_down);
        final ImageButton min_timeline2_down = (ImageButton) this.getViewPuppeteer().getmActivity().findViewById(R.id.min_timeline_2_down);

        this.viewPuppeteer.getmActivity().getSettingsScreen().getPerso_layout().pageScroll(View.FOCUS_UP);

        //Listener
        // Change the time of the graphic
        edit_hour_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0);
                if(hour == 10){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("minute_graph", (long)1);
                    }
                }
                else {
                    hour++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hours_graph", (long)hour);
            }
        });
        edit_hour_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0);
                if(hour == 1){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("minute_graph", (long)1);
                    }
                }
                else if(hour == 0) {
                    hour = 10;
                }
                else {
                    hour--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hours_graph", (long)hour);
            }
        });
        edit_min_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0);
                if(min == 59){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    min++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("minute_graph", (long)min);
            }
        });
        edit_min_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph",0);
                if(min == 1){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else if(min == 0){
                    min = 59;
                }
                else{
                    min--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("minute_graph", (long)min);
            }
        });

        // Change the time of the timeline
        hour_timeline1_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0);
                if(hour == 10){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_1", (long)1);
                    }
                }
                else {
                    hour++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hour_timeline_1", (long)hour);
            }
        });
        hour_timeline1_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0);
                if(hour == 1){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_1", (long)1);
                    }
                }
                else if(hour == 0) {
                    hour = 10;
                }
                else {
                    hour--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hour_timeline_1", (long)hour);
            }
        });
        min_timeline1_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1", 0);
                if(min == 59){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    min++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_1", (long)min);
            }
        });
        min_timeline1_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1", 0);
                if(min == 1){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else if(min == 0){
                    min = 59;
                }
                else{
                    min--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_1", (long)min);
            }
        });
        hour_timeline2_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0);
                if(hour == 10){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_2", (long)1);
                    }
                }
                else {
                    hour++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hour_timeline_2", (long)hour);
            }
        });
        hour_timeline2_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0);
                if(hour == 1){
                    hour = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2",0) == 0){
                        viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)hour, 1);
                        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_2", (long)1);
                    }
                }
                else if(hour == 0) {
                    hour = 10;
                }
                else {
                    hour--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)hour, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2",0));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("hour_timeline_2", (long)hour);
            }
        });
        min_timeline2_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2", 0);
                if(min == 59){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    min++;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_2", (long)min);
            }
        });
        min_timeline2_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2", 0);
                if(min == 1){
                    min = 0;
                    if(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0) == 0){
                        min = 1;
                        Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write a time greater than 00H00", Toast.LENGTH_LONG).show();
                    }
                }
                else if(min == 0){
                    min = 59;
                }
                else{
                    min--;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int)viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0), (int)min);
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("min_timeline_2", (long)min);
            }
        });

        // Change the color of the graphic
        color_fuchsia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getYoda().savedPreferences("colorGraph", "#8E3557");
            }
        });
        color_green.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getYoda().savedPreferences("colorGraph", "#34B536");
            }
        });
        color_red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getYoda().savedPreferences("colorGraph","#E83040");
            }
        });
        color_blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getYoda().savedPreferences("colorGraph","#ABD9FC");
            }
        });
        check_popUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int valuePopUp;
                if(check_popUp.isChecked()) {
                    valuePopUp = 1;
                }
                else{
                    valuePopUp = 2;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().changeCheckBoxNotification(valuePopUp, (int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("soundCheckBox", 1), (int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("vibratorpCheckBox", 1));
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("popUpCheckBox", (long) valuePopUp);
            }
        });
        check_sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int valueSound;
                if(check_sound.isChecked()) {
                    valueSound = 1;
                }
                else{
                    valueSound = 2;
                }
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("soundCheckBox", (long) valueSound);
            }
        });
        check_vibrator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int valueVibrator;
                if(check_vibrator.isChecked()) {
                    valueVibrator = 1;
                }
                else{
                    valueVibrator = 2;
                }
                viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("vibratorCheckBox", (long)valueVibrator);
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_down(edit_hour_down);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_up(edit_hour_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_down(edit_min_down);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_up(edit_min_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setColor_fuchsia(color_fuchsia);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setColor_blue(color_blue);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setColor_green(color_green);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setColor_red(color_red);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setCheck_popUp(check_popUp);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setCheck_sound(check_sound);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setCheck_vibrator(check_vibrator);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_timeline1_up(hour_timeline1_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_timeline2_up(hour_timeline2_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_timeline1_up(min_timeline1_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_timeline2_up(min_timeline2_up);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_timeline1_down(hour_timeline1_down);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setHour_timeline2_down(hour_timeline2_down);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_timeline1_down(min_timeline1_down);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMin_timeline2_down(min_timeline2_down);
    }

    /**
     * \brief Initialization of the connection interface (creation widget and listener)
     */
    private void initConnection(){
        this.viewPuppeteer.getmActivity().getSettingsScreen().setConnection((Button) this.viewPuppeteer.getmActivity().findViewById(R.id.button_connection));

        // Listener
        viewPuppeteer.getmActivity().getSettingsScreen().getConnection().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getConnection().startConnection(false);
            }
        });

        viewPuppeteer.getmActivity().getSettingsScreen().changeConnectionStatus(this.viewPuppeteer.getmActivity().getConnection().isConnected());
    }

    /**
     * \brief Instantiation of the widget and layout of the view
     */
    public void initInterface(){
        // Instantiation of the layout (each layout and the triangle to show)
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBaby_layout((RelativeLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.bb_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setSound_layout((LinearLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.sound_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setLight_layout((RelativeLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.brightness_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setPerso_layout((ScrollView) this.getViewPuppeteer().getmActivity().findViewById(R.id.perso_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setConnection_layout((RelativeLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.connection_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTriangle_baby((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.triangle_baby));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTriangle_sound((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.triangle_sound));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTriangle_light((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.triangle_light));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTriangle_perso((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.triangle_perso));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTriangle_co((ImageView) this.getViewPuppeteer().getmActivity().findViewById(R.id.triangle_connection));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setPerso_settings((RelativeLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.perso_settings));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMain_settings((RelativeLayout) this.getViewPuppeteer().getmActivity().findViewById(R.id.main_settings));

        // Open the baby information layout
        initBabyInfo();
        viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE, viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
        layout_open = "baby";
        viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE, viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
        viewPuppeteer.getmActivity().getSettingsScreen().checkRadioButton(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesString("gender", "neutral"));
    }

    /**
     * \brief Instantiation of the button or seekBar to listened
     */
	public void addSpyButtons(){
        // Create the txtView clickable to change the layout to display
        final TextView menu_baby = (TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.menu_baby);
        final TextView menu_sound = (TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.menu_sound);
        final TextView menu_light = (TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.menu_light);
        final TextView menu_perso = (TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.menu_perso);
        final TextView menu_co = (TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.menu_connection);

        // Listening on text view, show the good layout and close the last one
        menu_baby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initBabyInfo();
                changeBackgroundButton(menu_baby);
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE, viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
                layout_open = "baby";
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE, viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
                viewPuppeteer.getmActivity().getSettingsScreen().checkRadioButton(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesString("gender", "neutral"));
            }
        });
        menu_sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSound();
                changeBackgroundButton(menu_sound);
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE,  viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
                layout_open = "sound";
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE,  viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
                viewPuppeteer.getmActivity().getSettingsScreen().displayValueSound((int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("soundValue", SOUND_DEFAULT));
            }
        });
        menu_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initBrightness();
                changeBackgroundButton(menu_light);
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE,  viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
                layout_open = "light";
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE,  viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
                viewPuppeteer.getmActivity().getSettingsScreen().displayValueLight((int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("lightValue_dark_dim", LIGHT_DEFAULT_DARK_DIM), (int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("lightValue_dim_bright", LIGHT_DEFAULT_DIM_BRIGHT));
            }
        });
        menu_perso.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPerso();
                changeBackgroundButton(menu_perso);
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE,  viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
                layout_open = "perso";
                double graph_min = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("minute_graph", 0);
                double graph_hour = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hours_graph", 0);
                if((graph_hour == 0) && (graph_min == 0)){
                    graph_min = GRAPHIC_TIME_DEFAULT;
                }
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeGraphic((int) graph_hour, (int) graph_min);

                double timeline_min_1 = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1", 0);
                double timeline_hour_1 = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1", 0);
                if((timeline_min_1 == 0) && (timeline_hour_1 == 0)){
                    timeline_hour_1 = 2;
                }
                double timeline_min_2 = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2", 0);
                double timeline_hour_2 = viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2", 0);
                if((timeline_min_2 == 0) && (timeline_hour_2 == 0)){
                    timeline_hour_2 = 4;
                }

                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(1, (int)timeline_hour_1, (int)timeline_min_1);
                viewPuppeteer.getmActivity().getSettingsScreen().displayTimeTimeline(2, (int) timeline_hour_2, (int) timeline_min_2);

                viewPuppeteer.getmActivity().getSettingsScreen().changeCheckBoxNotification((int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("popUpCheckBox", 1), (int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("soundCheckBox", 1), (int) viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("vibratorCheckBox", 1));
                viewPuppeteer.getmActivity().getSettingsScreen().changeCheckBoxColor(viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesString("colorGraph","fuchsia"));
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE,  viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
            }
        });
        menu_co.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBackgroundButton(menu_co);
                initConnection();
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.GONE,  viewPuppeteer.getmActivity().getSettingsScreen().getPerso_settings());
                layout_open = "connection";
                viewPuppeteer.getmActivity().getSettingsScreen().changeVisibilityLayout(layout_open, View.VISIBLE,  viewPuppeteer.getmActivity().getSettingsScreen().getMain_settings());
            }
        });

        // Instantiation of the button listened
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMenu_baby(menu_baby);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMenu_sound(menu_sound);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMenu_light(menu_light);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMenu_perso(menu_perso);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setMenu_connection(menu_co);

        changeBackgroundButton(viewPuppeteer.getmActivity().getSettingsScreen().getMenu_baby());
    }

    /**
     * \brief Change the background of the button
     * \param button    The button to change
     */
    private void changeBackgroundButton(TextView button){
        viewPuppeteer.getmActivity().getSettingsScreen().getMenu_baby().setSelected(false);
        viewPuppeteer.getmActivity().getSettingsScreen().getMenu_sound().setSelected(false);
        viewPuppeteer.getmActivity().getSettingsScreen().getMenu_light().setSelected(false);
        viewPuppeteer.getmActivity().getSettingsScreen().getMenu_perso().setSelected(false);
        viewPuppeteer.getmActivity().getSettingsScreen().getMenu_connection().setSelected(false);
        button.setSelected(true);
    }

    /**
     * \brief Send new sound threshold
     */
    void sendNewSoundThreshold() {
        getViewPuppeteer().getmActivity().getSoundThreshold().setValueSound(soundValue);
        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("soundValue", Long.valueOf(soundValue));
    }

    /**
     * \brief Send new light threshold (between dark and dim)
     */
    void sendNewLightThreshold() {
        String lightThresholds;
        lightThresholds = lightValue_dark_dim+"/"+lightValue_dark_dim;
        getViewPuppeteer().getmActivity().getLightThreshold().setLightThresholds(lightThresholds);
        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("lightValue_dark_dim",Long.valueOf(lightValue_dark_dim));
        viewPuppeteer.getmActivity().getYoda().savedPreferencesLong("lightValue_dim_bright",Long.valueOf(lightValue_dim_bright));
    }


    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the ViewPuppeteer class
     */
    ViewPuppeteer getViewPuppeteer() {
		return viewPuppeteer;
	}

    /**
     * \brief Get method for the default value of brightness (between dark and dim)
     */
    public static int getLIGHT_DEFAULT_DARK_DIM() {
        return LIGHT_DEFAULT_DARK_DIM;
    }

    /**
     * \brief Get method for the default value of brightness (between dim and bright)
     */
    public static int getLIGHT_DEFAULT_DIM_BRIGHT() {
        return LIGHT_DEFAULT_DIM_BRIGHT;
    }

    /**
     * \brief Set method for the sound value
     */
    public void setSoundValue(int soundValue) {
        this.soundValue = soundValue;
    }

    /**
     * \brief Set method for the light value (between dark and dim)
     */
    public void setLightValue_dark_dim(int lightValue_dark_dim) {
        this.lightValue_dark_dim = lightValue_dark_dim;
    }

    /**
     * \brief Set method for the light value (between dim and bright)
     */
    public void setLightValue_dim_bright(int lightValue_dim_bright) {
        this.lightValue_dim_bright = lightValue_dim_bright;
    }
}
